import groovy.mock.interceptor.MockFor
import javax.swing.JFrame
import javax.swing.JPanel
import net.yiny.scrummer.ui.storyboard.CardBuilder
import net.yiny.scrummer.ui.storyboard.PhaseStripBuilder
import org.fest.swing.fixture.FrameFixture

scenario 'basic initialisation', {
    when 'we create a builder', {
        builder = new PhaseStripBuilder()
    }
    then 'it is created with no errors', {}
}

scenario 'initialisation with no children produces panel', {
    when 'we create and initialise a builder with no children', {
        builder = new PhaseStripBuilder()
        panel = builder.initialise()
    }
    then 'the resulting panel is not null and is of the right type', {
        panel.shouldNotBe null
        panel.shouldBeA JPanel
    }
}

scenario 'initialisation with two children produces panel and interacts', {
    given 'two child card builders', {
        mockChild1 = new MockFor(CardBuilder)
        childPanel1 = new JPanel()
        mockChild2 = new MockFor(CardBuilder)
        childPanel2 = new JPanel()
        mockChild1.demand.initialise() {childPanel1}
        mockChild2.demand.initialise() {childPanel2}
        child1 = mockChild1.proxyInstance()
        child2 = mockChild2.proxyInstance()
    }
    when 'the builder is set to have the two children and initialised', {
        builder.children = [child1, child2]
        panel = builder.initialise()
    }
    then 'the resulting panel is correct', {
        panel.shouldNotBe null
        panel.shouldBeA JPanel
    }
    then 'the initialisation should have been called on the children', {
        mockChild1.verify child1
        mockChild2.verify child2
    }
    then 'the panel returned should have the child panels as children', {
        panel.components.length.shouldBe 2
        panel.components[0].shouldBeEqualTo childPanel1
        panel.components[1].shouldBeEqualTo childPanel2
    }
}

scenario 'clicking on the panel should notify registered listeners', {
    when 'a builder is setup under the test fixture and given an event listener', {
        builder = new PhaseStripBuilder()
        frame = new JFrame()
        window = new FrameFixture(frame)
        frame.contentPane = builder.initialise()
        notified = false
        builder.addListener([mouseClicked: {notified = true }])
    }
    then 'showing the window and simulating a click should result in an event being sent to the listener', {
        window.show()
        window.panel().click()
        notified.shouldBe true
    }
    and 'finallu clean up the test fixture', {
        window.cleanUp()
    }
}
